%this function reshapes matrices into column vectors and concatenate them
%into a single matrix. Each matrix will be a column of the resultant matrix
%Uk: is the k-th matrix. All these matrices should be of the same size

%created by Ricardo Mejia-Alvarez. Urbana, IL. 09/25/09
%modified:  09/20/2010

function [U] = mixingMat(Umat)
L = size( Umat );

for k = 1 : L(3)
    
    if k ~= 1
        Uk = Umat( : , : , k);
        Uk = reshape(Uk , numel(Uk) , 1);
        U = [U,Uk]; %#ok<AGROW>
        
    else
        U = Umat( : , : , k);
        U = reshape(U , numel(U) , 1);
    end

end

U = sortrows(U , [2 , 1]);


end